#include "StdAfx.h"

#include "App.h"
#include "Log.h"


App::App(void) :
    m_bInitialized(false),
    m_bRunning(false),
    m_timer(0.0)
{
}


App::~App(void)
{
    shutdown();
}

bool App::init(void)
{
    if(!glfwInit())
    {
        LOG_ERROR("glfwInit failed");
        return false;
    }

    m_bInitialized = true;

    //GLFWvidmode vm[128];
    //int n = glfwGetVideoModes(vm, 128), i = 0;
    //LOG_ERROR("Printing all " << n << " video modes available: ");
    //while(i < n)
    //{
    //	const GLFWvidmode & vidmode = vm[i];
    //	LOG_ERROR("Video mode " << i << ": "
    //		<< vidmode.Width << 'x' << vidmode.Height
    //		<< ' ' << vidmode.RedBits << vidmode.GreenBits << vidmode.BlueBits
    //		<< std::endl);

    //	++i;
    //}

    /*!
    \todo Mover creacion de ventana a otro metodo?
    */
    if(!glfwOpenWindow(800, 600, 8, 8, 8, 8, 24, 8, GLFW_WINDOW))
    {
        LOG_ERROR("glfwOpenWindow failed");
	    shutdown();
		return false;
    }

    int major = glfwGetWindowParam(GLFW_OPENGL_VERSION_MAJOR);
    int minor = glfwGetWindowParam(GLFW_OPENGL_VERSION_MINOR);
    LOG_INFO("Using OpenGL " << major << '.' << minor);

    return true;
}

void App::shutdown(void)
{
    if(m_bInitialized)
    {
        glfwTerminate();
        m_bInitialized = false;
    }
}

void App::run(void)
{
    if(!m_bInitialized)
    {
        LOG_ERROR("App object not initialized. Not running.");
        return;
    }

    m_bRunning = true;
    while(m_bRunning)
    {
        // Main loop //////////////////////////////////////////////
        // Elapsed time since last loop iteration


        // Read from input class (pressed keys, mouse movement, etc)
        // and translate input to "commands"


        // Update "scene" based on time and input


        // Draw


        // Keep running?



        // End of main loop ////////////////////////////////////////

        //< Old loop, paints a black window ========================
        // GL rendering...
        glClear(GL_COLOR_BUFFER_BIT);

        // Buffer presenting. 60Hz by default, it seems
        // Implicitly calls glfwPollEvents (default behavoir).
        glfwSwapBuffers();

        // Keep running?
        m_bRunning = glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS &&
            glfwGetWindowParam(GLFW_OPENED);
    }
}
